package com.itheima.test;

import com.itheima.domain.Customer;
import com.itheima.utils.JpaUtil;
import org.junit.Test;

import javax.persistence.*;
import java.util.List;

public class MyJpa {
    @Test
    public void test() {
        /**
         * 创建实体管理类工厂，借助Persistence的静态方法获取
         * 		其中传递的参数为持久化单元名称，需要jpa配置文件中指定
         */
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("myJpa");
        //创建实体管理类
        EntityManager em = factory.createEntityManager();
        //获取事务对象
        EntityTransaction tx = em.getTransaction();
        //开启事务
        tx.begin();
        //创建实体类对象
        Customer c = new Customer();
        //设置实体类参数
        c.setCustName("小巷子救援大队队长");
        c.setCustAddress("吉山上街凉亭口正对第一条小巷子");
        c.setCustLevel("斗皇7阶");
        c.setCustIndustry("妇联协会");
        c.setCustPhone("110");
        c.setCustSource("凉亭情报员");
        //执行保存操作
        em.persist(c);
        //提交事务
        tx.commit();
        //释放资源
        em.close();
    }

    @Test
    public void test2() {
        EntityManager em = JpaUtil.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        //开启事务
        tx.begin();
        //创建实体类对象
        Customer c = new Customer();
        //设置实体类参数
        c.setCustName("小巷子救援大队队长");
        c.setCustAddress("吉山上街凉亭口正对第一条小巷子");
        c.setCustLevel("斗皇7阶");
        c.setCustIndustry("妇联协会");
        c.setCustPhone("110");
        c.setCustSource("凉亭情报员");
        //执行保存操作
        em.persist(c);
        //提交事务
        tx.commit();
        //释放资源
        em.close();
    }

    /**
     * 保存操作
     */
    @Test
    public void testAdd() {
        EntityManager em = JpaUtil.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        //创建实体类对象
        Customer c = new Customer();
        //设置实体类参数
        c.setCustName("小巷子救援大队队长");
        c.setCustAddress("吉山上街凉亭口正对第一条小巷子");
        c.setCustLevel("斗皇7阶");
        c.setCustIndustry("妇联协会");
        c.setCustPhone("110");
        c.setCustSource("凉亭情报员");
        try {
            //使用工具类创建实体管理器对象
            em = JpaUtil.createEntityManager();
            //获取事务对象
            tx = em.getTransaction();
            //开启事务
            tx.begin();
            //执行保存操作
            em.persist(c);
            //提交事务
            tx.commit();
        } catch (Exception e) {
            //回滚事务
            tx.rollback();
            e.printStackTrace();
        } finally {
            //释放资源
            em.close();
        }
    }

    /**
     * 查询一个对象操作,立即查询
     */
    @Test
    public void testFindOne() {
        EntityManager em = JpaUtil.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        //==============================================
        //进行CRUD操作: EntityManager 与数据库交互
        Customer c = em.find(Customer.class, 2L);
        //Customer c = em.getReference(Customer.class, 1L);
        System.out.println(c);

        //==============================================
        tx.commit();
        em.close();
    }

    /**
     * 查询一个延迟加载
     */
    @Test
    public void testFindOneLazy() {
        // 定义对象
        EntityManager em = null;
        EntityTransaction tx = null;
        try {
            // 获取实体管理对象
            em = JpaUtil.createEntityManager();
            // 获取事务对象
            tx = em.getTransaction();
            // 开启事务
            tx.begin();
            // 执行操作
            Customer c1 = em.getReference(Customer.class, 1L);
            // 提交事务
            tx.commit();
            System.out.println(c1);
        } catch (Exception e) {
            // 回滚事务
            tx.rollback();
            e.printStackTrace();
        } finally {
            // 释放资源
            em.close();
        }
    }

    /**
     * 查询全部
     */
    @Test
    public void testFindAll() {
        EntityManager em = JpaUtil.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        String jpql = "from Customer";
        Query query = em.createQuery(jpql);
        List list = query.getResultList();
        for (Object o : list) {
            Customer c = (Customer) o;
            System.out.println(c);
        }
        tx.commit();
        em.close();
    }

    /**
     * 分页查询
     */
    @Test
    public void testPage() {
        EntityManager em = JpaUtil.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        String jpql = "from Customer";
        Query query = em.createQuery(jpql);
        query.setFirstResult(2);
        query.setMaxResults(2);
        List list = query.getResultList();
        for (Object o : list) {
            Customer c = (Customer) o;
            System.out.println(c);
        }
        tx.commit();
        em.close();
    }

    /**
     * 条件查询
     */
    @Test
    public void testCondition() {
        EntityManager em = JpaUtil.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        String jpql = "from Customer where custName like ?";
        Query query = em.createQuery(jpql);
        query.setParameter(1, "%巷子%");
        List list = query.getResultList();
        for (Object o : list) {
            Customer c = (Customer) o;
            System.out.println(c);
        }
        tx.commit();
        em.close();
    }

    /**
     * 排序查询
     */
    @Test
    public void testOrderBy() {
        EntityManager em = JpaUtil.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        String jpql = "from Customer order by custId desc";
        Query query = em.createQuery(jpql);
        List list = query.getResultList();
        for (Object o : list) {
            Customer c = (Customer) o;
            System.out.println(c);
        }
        tx.commit();
        em.close();
    }

    /**
     * 统计查询
     */
    @Test
    public void testCount() {
        EntityManager em = JpaUtil.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        String jpql = "select count(custId) from Customer";
        Query query = em.createQuery(jpql);
        Long c = (Long) query.getSingleResult();
        System.out.println(c);
        tx.commit();
        em.close();
    }

    /**
     * 修改操作
     */
    @Test
    public void testUpdate() {
        EntityManager em = JpaUtil.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        try {
            em = JpaUtil.createEntityManager();
            tx = em.getTransaction();
            tx.begin();
            Customer c = em.find(Customer.class, 1L);
            c.setCustLevel("九星斗皇");
            em.merge(c);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            e.printStackTrace();
        } finally {
            em.close();
        }
    }

    /**
     * 删除操作
     */
    @Test
    public void testDelete() {
        EntityManager em = JpaUtil.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        try {
            em = JpaUtil.createEntityManager();
            tx = em.getTransaction();
            tx.begin();
            Customer c = em.find(Customer.class, 2L);
            em.remove(c);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            e.printStackTrace();
        } finally {
            em.close();
        }
    }
}
